package zhenbang
{
	import flash.events.Event;
	import flash.desktop.NativeApplication;
	import flash.display.Sprite;
	import flash.geom.Rectangle;

	public class PassWordInput extends Sprite
	{
		private static var _main:PassWordInput;
		public static function get main():PassWordInput
		{
			if(_main==null)
				_main = new PassWordInput();
			return _main;
		}
		private static var txt:TxtInput;
		public function PassWordInput()
		{
			_main = this;
			with(graphics)
			{
				graphics.beginFill(0x0,.7);
				graphics.drawRect(0,0, test.stageW, test.stageH);
				graphics.endFill();
			}
			txt = new TxtInput(45,45*10,"请输入密码:",null,null,0xee9900);
			txt.displayAsPassword = true;
			addChild(txt);
			var commitBtn:TxtBtn = new TxtBtn("确定",ok);
			commitBtn.setformat(new Rectangle(0,0,100,50),0x990000,0x990000,0xffffff,0xffffff,0xee9900);
			commitBtn.x = txt.x + txt.width + 10;
			addChild(commitBtn);

			var i:int = 0;
			while(i<10)
			{
				var btn0:TxtBtn = makeNumberBtn(String(i));
				btn0.y = txt.y + txt.height+10;
				btn0.x = i*52;
				++i;
			}
			var btn1:TxtBtn = makeNumberBtn("←");
			btn1.y = txt.y + txt.height + 10;
			btn1.x = i*52;
		}

		private function makeNumberBtn(s:String):TxtBtn
		{
			var btn:TxtBtn = new TxtBtn(s,appendTxt,s);
			btn.setformat(new Rectangle(0,0,50,50),0x990000,0x990000,0xffffff,0xffffff,0xee9900);
			addChild(btn);
			return btn;
		}

		private function appendTxt(s:String):void
		{
			if(s=="←")
			{
				if(txt.value.length>0)
					txt.value = txt.value.substr(0,txt.value.length-1);
			}else{
				txt.value += s;
			}
		}

		private static function get password():String
		{
			if(Datas.jsonObject)
			{
				return Datas.jsonObject.password;
			}
			CONFIG::debugging{
				return "";
			}
			return "111111";
		}

		private static var index:int = 0;
		private function ok(e:Event=null):void
		{
			if(password==(txt.value).replace(/[\r\n\s]/mg,""))
			{
				if( Server.serverSocket && Server.serverSocket.bound ) 
				{
					Server.serverSocket.close();
				}
				NativeApplication.nativeApplication.exit();
			}else{
				Toast.show("密码错误");
			}
			++index;
			if(index>=3)//密码错误3次
			{
				FirstPage.show();
				visible = false;
			}
		}


		public static function show():void
		{	
			test.main.addChild(main);
			main.visible = true;
			index = 0;
			txt.value= "";

		}
	}
}
